import { useRemStore } from '@/store/remStore.js'
/**
 * rem初始化
 * @param {设计稿件宽度} PAGE_WIDTH 
 * @param {设计稿件1rem对应尺寸} PAGE_FONT_SIZE 
 */
export function remInit() {
    var timer = null;

    window.addEventListener('resize', function () {
        if (timer) clearTimeout(timer);
        timer = setTimeout(onResize, 100);
    });
    onResize();
}

export function onResize() {
    document.documentElement.style.fontSize = calcFontSize() + 'px';
}

function calcFontSize(PAGE_WIDTH, PAGE_FONT_SIZE) {
    const remStore = useRemStore();
    PAGE_WIDTH = PAGE_WIDTH || remStore.pageWidth;
    PAGE_FONT_SIZE = PAGE_FONT_SIZE || remStore.pageFontSize;
    let val;
    val = PAGE_FONT_SIZE * document.documentElement.clientWidth / PAGE_WIDTH;
    return val
}

/**
 * rem to px
 * @param {输入尺寸} val 
 * @param {设计稿件宽度} PAGE_WIDTH 
 * @param {设计稿件1rem对应尺寸} PAGE_FONT_SIZE 
 * @returns 
 */
export function fontSize(val, PAGE_WIDTH, PAGE_FONT_SIZE) {
    const remStore = useRemStore();
    PAGE_WIDTH = PAGE_WIDTH || remStore.pageWidth;
    PAGE_FONT_SIZE = PAGE_FONT_SIZE || remStore.pageFontSize;
    let result = val;
    const fontSize = calcFontSize(PAGE_WIDTH, PAGE_FONT_SIZE);
    result = (val / PAGE_FONT_SIZE) * fontSize;
    return result
}